use Wx 0.15 qw[:allclasses];
use strict;
package wxTreeWindow;
use Wx qw[:everything];

our @ISA = ("Wx::Panel", "Bio::Phylo::Treedrawer");
use strict;
use Bio::Phylo;
use Bio::Phylo::IO;
use Bio::Phylo::Treedrawer;

#use MyTreedrawer;
use Bio::Phylo::IO 'parse';
use Image::LibRSVG;
use Data::Dumper;

sub new {
    my( $self, $parent, $id, $pos, $size, $style, $name ) = @_;

    #Set default arguments
    $parent = undef                 unless defined $parent;
    $id     = -1                    unless defined $id;
    $pos    = wxDefaultPosition     unless defined $pos;
    $size   = wxDefaultSize         unless defined $size;
    $name   = ""                    unless defined $name;
    $style = wxDEFAULT_FRAME_STYLE  unless defined $style;

    #If the panel is resized a full redraw is necessary
    $style = $style | wxFULL_REPAINT_ON_RESIZE;

    #call the constructor of the wxPanel.
    $self = $self->SUPER::new( $parent, $id, $pos, $size, $style, $name );

    #Register the painting event
    Wx::Event::EVT_PAINT($self,  \&OnPaint);

    #Get the panel width and height
    my ($width, $height) = $self->GetSizeWH();

    #Currently I create a tree drawer and copy the settings over. 
    #Alternatively 1. Could just replicate the functionality in TreeDrawer::new
    #                 but the current method ensures better compatibility
    #              2. Could use the new methods in both inherited classes, but 
    #                 I had trouble getting this to work.
    my $treedrawer = Bio::Phylo::Treedrawer->new(
#    my $treedrawer = Bio::Phylo::Treedrawer->new(
        -width  => $width,
        -height => $height,
        #-shape  => 'CURVY', # curvogram
        -shape  => 'DIAG', # diagonal lines
        #-shape  => 'RECT', # diagonal lines
#        -mode   => 'CLADO', # cladogram
        -mode   => 'PHYLO', # cladogram
        -format => 'WxDC'
    );

    #Copy the treedrawer fields to $self
    foreach my $key (keys %{$treedrawer}) { $self->{$key} = $treedrawer->{$key} }

    #Is a tree currently selected?
    $self->{treeSelected} = 0;

    bless $self, 'wxTreeWindow';
    return $self;
}

sub set_tree {
    my ($self, $tree) = @_;
    unless ($tree) {
        #The tree has been unselected
        $self->{treeSelected} = 0;
    } else {
        #A tree has been selected, set it using the parent method
        $self->{treeSelected} = 1;
        $self->SUPER::set_tree($tree);
    }

    #Draw the new tree (or absence of one)
    $self->OnPaint;
}

#Set a message to be displayed if no tree is selected
#input: the message, nothing to clear message
sub set_no_tree_msg {
	my $self = shift;
	my $msg = shift;
	if (defined $msg) {
		$self->{no_tree_msg} = $msg;
	} else {
		delete $self->{no_tree_msg};
	}
}

sub OnPaint {
    my ($self, $event) = @_;
 
    my $drawArea;

    #Depending whether the event is being called due to a change in the tree and 
    #not a redraw event we use different DC objects. Unsure how important this 
    #really is
    if (defined $event)
    {
        $drawArea = Wx::PaintDC->new($self);
    } else {
        $drawArea = Wx::ClientDC->new($self);
    }

    #Get the default background colour
    my $backColour = Wx::SystemSettings::GetColour(wxSYS_COLOUR_BACKGROUND);
    #$backColour = wxBLACK;
    #Create a brush with that colour
    my $brush = Wx::Brush->new($backColour, wxSOLID);
    #Set the brush as the default background brush
    $drawArea->SetBackground($brush);
    #Clear the panel
    $drawArea->Clear;

    #No tree selected
    if ($self->{treeSelected}==0) {
    	#Do we have to print a no tree text?
    	if (defined $self->{no_tree_msg}) {
    		my ($t_x, $t_y) = $drawArea->GetTextExtent($self->{no_tree_msg},$drawArea->GetFont);
    		my ($x, $y) = $drawArea->GetSizeWH();
    		$drawArea->DrawText($self->{no_tree_msg},
    				$x/2 - $t_x/2,
    				$y/2 - $t_y/2);
    	}
        return;
    }

    #Get the dimensions
    my ($width, $height) = $self->GetSizeWH();

    $self->set_scale_options(
        -width => '100%',
        -major => '10%', # major cross hatch interval
        -minor => '2%',  # minor cross hatch interval
    );

    #Set the dimensions for the treedrawer
    $self->set_width($width);   
    $self->set_height($height);   

    #Draw and convert the tree to a bitmap
    my $svntree = $self->draw($drawArea);
     #my $rsvg = new Image::LibRSVG();
     #$rsvg->loadImageFromString($svntree);
     #$rsvg->saveAs( "tempTree.png" );   
     #my $bitmap = $rsvg->getImageBitmap();
    #my $wxbitmap = Wx::Bitmap->new("tempTree.png",wxBITMAP_TYPE_PNG);

    #$drawArea->DrawBitmap($wxbitmap,0,0,0);

}

1;